//
//  UploadBriefcaseViewController.m
//  MISCloud
//
//  Created by goldeney on 13-8-29.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "OAUploadBriefcaseViewController.h"

#import "GEDateTextField.h"
#import "GEPickerTextField.h"
#import "FileUploadModel.h"
#import "FileSizeFormatHelper.h"
#import "FileTypeIcon.h"
#import "GEObjectButton.h"
#import "GEInputTableViewCell.h"
#import "GEPickerTextField.h"

#import "ServiceConnect.h"
#import "UserLoginNSCoding.h"
#import "NSKeyedArchiverUserLogin.h"
#import "NSDictionary+DataFormat.h"

#import "UploadViewController.h"
#import "ThreeDataViewController.h"
#import "PreviewLocalFileHelper.h"

@interface OAUploadBriefcaseViewController()

@property (nonatomic, strong) NSMutableArray *briefcaseArray;
@property (nonatomic, strong) NSMutableArray *accessoryArray;

@property (nonatomic, strong) NSArray *sendPeopleArray;

@end


@implementation OAUploadBriefcaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"新建公文"];
        
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self
                                                                      action:@selector(uploadBriefcaseSelector:)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.briefcaseArray = [NSMutableArray array];
    self.accessoryArray = [NSMutableArray array];
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//添加文件
- (void)drawRowFileCell:(UITableViewCell*)cell model:(FileUploadModel *)model indexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize = cell.frame.size;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, cellSize.height - 10, cellSize.height - 10)];
    imageView.image = [FileTypeIcon setFileTypeIcon:model.filePath];
    [cell.contentView addSubview:imageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(cellSize.height + 5, 3, cellSize.width - cellSize.height - 60, cellSize.height/2)];
    titleLabel.text = model.fileName;
    titleLabel.font = [UIFont systemFontOfSize:15.0];
    titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.textColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:titleLabel];
    
    NSString *fileSize = [FileSizeFormatHelper getFileSizeString:[NSString stringWithFormat:@"%lld",model.fileSize]];
    UILabel *field = [[UILabel alloc] initWithFrame:CGRectMake(cellSize.height + 5, cellSize.height/2 - 2, cellSize.width - cellSize.height - 10, cellSize.height/2)];
    field.font = [UIFont systemFontOfSize:12.0];
    field.text = [NSString stringWithFormat:@"文件大小：%@", fileSize] ;
    field.textColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:field];
    
    GEObjectButton *button = [GEObjectButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(cellSize.width - 50, 0, cellSize.height, cellSize.height);
    [button setImage:[UIImage imageNamed:@"del.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(delFileSelector:) forControlEvents:UIControlEventTouchUpInside];
    button.object = @[@(indexPath.section), model];
    [cell.contentView addSubview:button];
    
    //添加分割线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 1, SuperWidth, 1)];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [cell.contentView addSubview:lineView];
}

#pragma mark - UIAlertView

- (void)showAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}


#pragma mark
#pragma mark - 上传文件

- (void)uploadBriefcaseSelector:(id)selector
{
    //关闭键盘
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    
    GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    NSDictionary *briefcaseType = textField.selectData;
    
    UITextField *briefcaseNameField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    NSString *briefcaseName = [briefcaseNameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    UITextField *briefcaseTimeField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    NSString *briefcaseTime = [briefcaseTimeField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    if (!(self.sendPeopleArray && self.sendPeopleArray.count>0)) {
        [self showAlertView:@"请选择发送对象"];
    } else if (!(briefcaseType && briefcaseType.count>0)){
        [self showAlertView:@"请选择公文类型"];
    } else if (briefcaseName.length <= 0){
        [self showAlertView:@"请输入公文名称"];
    } else if (briefcaseTime.length <= 0){
        [self showAlertView:@"请选择公文时效"];
    } else if (!(self.briefcaseArray && self.briefcaseArray.count>0)){
        [self showAlertView:@"请添加公文原件（必填）"];
    } else {
        //发送公文
        [self getNetWorkDataBriefcaseSend];
    }
    
}

- (void)getNetWorkDataBriefcaseSend
{
    //获取用户信息
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    
    //获取发送对象
    NSMutableString *postIDs = [NSMutableString string];
    for (NSMutableDictionary *mDictionary in self.sendPeopleArray)
        [postIDs appendFormat:@"%@,",[mDictionary objectFormatForKey:@"id"]];
    if(postIDs.length>0) [postIDs deleteCharactersInRange:NSMakeRange(postIDs.length - 1, 1)];
    
    GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    NSDictionary *briefcaseType = textField.selectData;
    
    UITextField *briefcaseNameField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    NSString *briefcaseName = [briefcaseNameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    UITextField *briefcaseTimeField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    NSString *briefcaseTime = [briefcaseTimeField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    UITextField *briefcaseRemarkField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:0];
    NSString *briefcaseRemark = [briefcaseRemarkField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    //公文上传文件信息
    FileUploadModel *model = [self.briefcaseArray firstObject];
    NSDictionary *jsonDictionary = [model getFileInfo];
    
    //公文实体JSON
    NSMutableDictionary *mDictionary = [NSMutableDictionary dictionary];
    [mDictionary setObject:[briefcaseType objectFormatStringForKey:@"id"] forKey:@"officiaType"];
    [mDictionary setObject:briefcaseName forKey:@"officialName"];
    [mDictionary setObject:briefcaseTime forKey:@"timeLimit"];
    [mDictionary setObject:[jsonDictionary objectFormatStringForKey:@"filePath"] forKey:@"officialPath"];
    [mDictionary setObject:@"" forKey:@"officialHtmlPath"];
    [mDictionary setObject:briefcaseRemark forKey:@"remark"];
    
    NSString *jsonString = nil;
    if ([NSJSONSerialization isValidJSONObject:mDictionary]) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mDictionary options:NSJSONWritingPrettyPrinted error:&error];
        jsonString =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    //处理附件信息
    NSString *jsonFileString = nil;
    NSMutableArray *jsonFileArray = [NSMutableArray array];
    for(FileUploadModel *model in self.accessoryArray){
        NSDictionary *jsonDictionary = [model getFileInfo];
        NSMutableDictionary *mDictionary = [NSMutableDictionary dictionary];
        [mDictionary setObject:[jsonDictionary objectFormatForKey:@"filePath"] forKey:@"FJPath"];
        [mDictionary setObject:[jsonDictionary objectFormatForKey:@"fileName"] forKey:@"FJName"];
        [jsonFileArray addObject:mDictionary];
    }
    if ([NSJSONSerialization isValidJSONObject:jsonFileArray]) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonFileArray options:NSJSONWritingPrettyPrinted error:&error];
        jsonFileString =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    
    //0、已发送公文，1、收到公文
    ServiceParametModel *wbsModel = [[ServiceParametModel alloc] init];
    [wbsModel setValue:userInfo.Id forKey:@"userid"];
    [wbsModel setValue:postIDs forKey:@"postidList"];
    [wbsModel setValue:jsonString forKey:@"jsonData"];
    [wbsModel setValue:jsonFileString forKey:@"fjList"];
    
    ServiceConnect *content = [[ServiceConnect alloc] initWithMethodName:@"OA_DocumentSend" dataArray:wbsModel];
    [content showMBProgressHUD:self.navigationController labelText:@"正在发送"];
    [content startConnectNetworkCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        if(result.resultArray && result.resultArray.count>0){
            //result：返回结果(1成功，0失败)
            NSDictionary *resultArray = [result.resultArray firstObject];
            NSInteger result = [[resultArray objectFormatForKey:@"result"] integerValue];
            if (result == 0) [self showAlertView:@"提交失败，请重新尝试"];
            else if (result == -2) [self showAlertView:@"公文名称已存在，请重新输入公文名称"];
            else [self successUpLoadBriefcase];
            
        } else {
            [self showAlertView:@"提交失败，请重新尝试"];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==failed===%@",error);
        [self showAlertView:@"发送出错了，请重新尝试"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"===abnormal==%@",error);
        [self showAlertView:@"没有连接网络，请检查网络是否连接"];
    }];
}

- (void)successUpLoadBriefcase
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"公文发送成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}


#pragma mark
#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0) return 5;
    else if(section == 1) return self.briefcaseArray.count <= 0 ? 1 : self.briefcaseArray.count;
    else if(section == 2) return self.accessoryArray.count + 1;
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"OAUploadBriefcaseViewCell%ld%ld",indexPath.section,indexPath.row];
    if(indexPath.section == 0){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        //页面数据处理
        if(indexPath.row == 0){
            //机构数类型(1:公文机构职位树,2:消息管理机构职位树,3:通讯录关联公司列表树)
            UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
            ServiceParametModel *model = [[ServiceParametModel alloc] init];
            model.methodName = @"OA_GetTree_Public";
            [model setValue:userInfo.Id forKey:@"userid"];
            [model setValue:@1 forKey:@"type"];
        
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:model];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"发送对象";
            cell.placeholder = @"请选择发送对象";
            cell.isMark = YES;
            cell.isEdit = NO;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            //获取公文类型
            ServiceParametModel *model = [[ServiceParametModel alloc] init];
            model.methodName = @"OA_SelDocumentType";
            
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier model:model];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"公文类型";
            cell.placeholder = @"请选择公文类型";
            cell.isMark = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"公文名称";
            cell.placeholder = @"请输入公文名称";
            cell.isMark = YES;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithDateReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"有效时限";
            cell.placeholder = @"请选择有效时限";
            cell.isMark = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 4){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"备      注";
            cell.placeholder = @"可不填";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        return cell;
    }
    
    //添加公文原件
    else if(indexPath.section == 1){
        GEInputTableViewCell *cell = [[GEInputTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.bounds = CGRectMake(0, 0, cell.bounds.size.width, 48);
        
        if (self.briefcaseArray.count > 0) {
            FileUploadModel *model = [self.briefcaseArray objectAtIndex:indexPath.row];
            [self drawRowFileCell:cell model:model indexPath:indexPath];
        } else {
            UIImage *image = [UIImage imageNamed:@"button_bg_add"];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.size.width/2-image.size.width/2, cell.frame.size.height/2-image.size.height/2, image.size.width, image.size.height)];
            [imageView setImage:image];
            [cell.contentView addSubview:imageView];
        }
        return cell;
    }
    
    //添加附件
    else if(indexPath.section == 2){
        GEInputTableViewCell *cell = [[GEInputTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.bounds = CGRectMake(0, 0, cell.bounds.size.width, 48);
        
        if (indexPath.row < self.accessoryArray.count) {
            FileUploadModel *model = [self.accessoryArray objectAtIndex:indexPath.row];
            [self drawRowFileCell:cell model:model indexPath:indexPath];
        } else {
            UIImage *image = [UIImage imageNamed:@"button_bg_add"];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.size.width/2-image.size.width/2, cell.frame.size.height/2-image.size.height/2, image.size.width, image.size.height)];
            [imageView setImage:image];
            [cell.contentView addSubview:imageView];
        }
        return cell;
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //基本信息不添加
    if(section == 0) return nil;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 30)];
    view.backgroundColor = [UIColor clearColor];
    
    UIView *viewBackground = [[UIView alloc] initWithFrame:CGRectMake(10, 0, SuperWidth - 20, 30)];
    viewBackground.backgroundColor = [UIColor whiteColor];
    [view addSubview:viewBackground];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SuperWidth - 30, 29.5)];
    lable.textColor = [UIColor darkGrayColor];
    lable.font = [UIFont boldSystemFontOfSize:15];
    if (section == 1) {
        lable.text = @"添加公文原件";
    } else if(section == 2){
        lable.text = @"添加附件";
    }
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29, SuperWidth, 1)];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view addSubview:lineView];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) return 0;
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 0) return 48;
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //发送对象
    if(indexPath.section == 0 && indexPath.row == 0) {
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        NSArray *sendPeopleArray = geViewCell.objectData;
        
        if (sendPeopleArray) {
            ThreeDataViewController *view = [[ThreeDataViewController alloc] initWithStyle:SelectMulti treeData:sendPeopleArray];
            [self.navigationController pushViewController:view animated:YES];
            view.backResultDelagate = self;
            [view setTitle:@"选择发送对象"];
            [view setTagKey:@"SendPeople"];
        } else {
            [geViewCell reloadData];
        }
    }
    
    //公文类型
    if(indexPath.section == 0 && indexPath.row == 1) {
        GEInputTableViewCell *geViewCell = (GEInputTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        NSArray *resultArray = geViewCell.objectData;
        
        if (resultArray) {
            NSDictionary *QDDJ = [NSDictionary dictionaryWithObject:@"%@" forKey:@"mc"];
            [geViewCell setPickerTextFieldComponents:resultArray keyValue:@[QDDJ]];
        } else {
            [geViewCell reloadData];
        }
    }
    
    //添加公文原件
    else if (indexPath.section == 1 && (!self.briefcaseArray || self.briefcaseArray.count<=0)
             && indexPath.row == ([self tableView:nil numberOfRowsInSection:1] -1)) {
        UploadViewController *abstractView = [[UploadViewController alloc] init];
        [abstractView setNumberType:@"1" extension:@"doc,docx"];
        
        abstractView.key = @"BriefcaseFile";
        abstractView.backResultDelagate = self;
        [self.navigationController pushViewController:abstractView animated:YES];
    }
    
    //添加附件
    else if (indexPath.section == 2 && indexPath.row ==([self tableView:nil numberOfRowsInSection:2] - 1)) {
        UploadViewController *abstractView = [[UploadViewController alloc] init];
        [abstractView setNumberType:@"2" extension:nil];
        
        abstractView.key = @"AccessoryFile";
        abstractView.backResultDelagate = self;
        [self.navigationController pushViewController:abstractView animated:YES];
    }
    
    //公文预览
    else if (indexPath.section == 1){
        FileUploadModel *model = [self.briefcaseArray objectAtIndex:indexPath.row];
        PreviewLocalFileHelper *preview = [PreviewLocalFileHelper instantiation];
        [preview previewViewController:self localFile:model.filePath];
    }
    
    //附件预览
    else if (indexPath.section == 2){
        FileUploadModel *model = [self.accessoryArray objectAtIndex:indexPath.row];
        PreviewLocalFileHelper *preview = [PreviewLocalFileHelper instantiation];
        [preview previewViewController:self localFile:model.filePath];
    }
    
    //键盘事件处理
    if(indexPath.section == 0) {
        UITextField *textField = [self getCellClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
        if (textField) [textField becomeFirstResponder];
    }
    
}


#pragma mark -
#pragma mark Button Event

- (void)delFileSelector:(GEObjectButton*)button
{
    NSArray *array = button.object;
    NSInteger section = [array[0] integerValue];
    id delValue = array[1];
    
    if (section == 1 && self.briefcaseArray.count>0) {
        NSInteger index = [self.briefcaseArray indexOfObject:delValue];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:1];
        
        NSArray *delIndexPath = [NSArray arrayWithObjects:indexPath, nil];
        [self.briefcaseArray removeObject:delValue];
        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:delIndexPath withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView endUpdates];
        
    } else if (section == 2 && self.accessoryArray.count>0) {
        NSInteger index = [self.accessoryArray indexOfObject:delValue];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:2];
        
        NSArray *delIndexPath = [NSArray arrayWithObjects:indexPath, nil];
        [self.accessoryArray removeObject:delValue];
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:delIndexPath withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView endUpdates];
    }
    
}

#pragma mark -
#pragma mark Get UITableViewCell

- (id)getCellClass:(Class)mClass pathForRow:(NSInteger)row inSection:(NSInteger)section
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    UITableViewCell *tableCell = [self.tableView cellForRowAtIndexPath:indexPath];
    for (id view in tableCell.contentView.subviews) {
        if ([view isKindOfClass:mClass]) {
            return view;
        }
    }
    return nil;
}

#pragma mark -
#pragma mark BackResultDataDelagate

- (void)backResultDataKey:(id)key dataValue:(id)backValue
{
    [super backResultDataKey:key dataValue:backValue];
    NSArray *array = backValue;
    if (!(array && array.count>0)) return;
    
    if([key isEqualToString:@"SendPeople"]){
        //发送对象
        self.sendPeopleArray = array;
        NSDictionary *mDictionary = [array firstObject];
        NSMutableString *value = [NSMutableString string];
        [value appendFormat:@"%@等%ld人",[mDictionary objectForKey:@"nodeName"],array.count];
        
        UITextField *textField = [self getCellClass:[UITextField class] pathForRow:0 inSection:0];
        textField.text = value;
        
    } else if([key isEqualToString:@"BriefcaseFile"]){
        //公文原件
        if ([backValue isKindOfClass:[NSArray class]]) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
            NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];
            [self.briefcaseArray insertObject:[array firstObject] atIndex:0];
            
            [self.tableView beginUpdates];
            [self.tableView reloadRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationTop];
            [self.tableView endUpdates];
            
            //设置文本信息
            FileUploadModel *model = [array firstObject];
            UITextField *textField = [self getCellClass:[UITextField class] pathForRow:2 inSection:0];
            if(textField.text.length<=0) textField.text = model.fileName;
        }
        
    } else if([key isEqualToString:@"AccessoryFile"]){
        //添加附件
        if ([backValue isKindOfClass:[NSArray class]]) {
            NSMutableArray *insertIndexPath = [NSMutableArray array];
            for (int i=0;i<array.count;i++) {
                id value = [array objectAtIndex:i];
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:2];
                [insertIndexPath addObject:indexPath];
                [self.accessoryArray insertObject:value atIndex:0];
            }
            
            if (insertIndexPath.count>0) {
                [self.tableView beginUpdates];
                [self.tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationTop];
                [self.tableView endUpdates];
            }
        }
    }
}


@end
